package com.cskaoyan.wx.service;

import com.cskaoyan.bean.Collect;
import com.cskaoyan.bean.CollectExample;
import com.cskaoyan.bean.Goods;
import com.cskaoyan.mapper.CollectMapper;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.utils.UserUtils;
import com.cskaoyan.wx.bo.WxCollectBO;
import com.cskaoyan.wx.vo.WxAddCollectVO;
import com.cskaoyan.wx.vo.WxCollectVO;
import com.cskaoyan.wx.vo.WxCouponVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WxCollectServiceImpl implements WxCollectService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    CollectMapper collectMapper;
    @Autowired
    GoodsMapper goodsMapper;

    /**
     * 收藏列表
     *
     * @param type
     * @param page
     * @param size
     * @return
     */
    @Override
    public WxCollectVO getCollectList(Byte type, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        Integer id = UserUtils.getUserId();

        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andTypeEqualTo(type);
        criteria.andUserIdEqualTo(id);
        List<Collect> collects = collectMapper.selectByExample(collectExample);

        ArrayList<WxCollectVO.CollectListBean> collectListBeans = new ArrayList<>();
        for (Collect collect : collects) {
            Integer valueId = collect.getValueId();
            Goods goods = goodsMapper.selectByPrimaryKey(valueId);

            WxCollectVO.CollectListBean collectListBean = new WxCollectVO.CollectListBean();
            collectListBean.setBrief(goods.getBrief());
            collectListBean.setPicUrl(goods.getPicUrl());
            collectListBean.setValueId(valueId);
            collectListBean.setName(goods.getName());
            collectListBean.setId(collect.getId());
            collectListBean.setType(type);
            collectListBean.setRetailPrice(goods.getRetailPrice());

            collectListBeans.add(collectListBean);
        }

        PageInfo<Collect> collectPageInfo = new PageInfo<>(collects);
        long total = collectPageInfo.getTotal();

        WxCollectVO wxCollectVO = new WxCollectVO();
        wxCollectVO.setTotalPages((int) total);
        wxCollectVO.setCollectList(collectListBeans);

        return wxCollectVO;

    }

    /**
     * 添加收藏
     *
     * @param wxCollectBO
     * @return
     */
    @Override
    public Integer addCollect(WxCollectBO wxCollectBO) {
        Integer id = UserUtils.getUserId();
        //判断商品是否已存在
        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andUserIdEqualTo(id);
        criteria.andValueIdEqualTo(wxCollectBO.getValueId());
        criteria.andTypeEqualTo(wxCollectBO.getType());
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        long l = collectMapper.countByExample(collectExample);
        if (l == 0) {
            //不存在
            Collect collect = new Collect();
            collect.setId(null);
            collect.setUserId(id);
            collect.setValueId(wxCollectBO.getValueId());
            collect.setType(wxCollectBO.getType());
            collect.setAddTime(new Date());
            collect.setUpdateTime(null);
            collect.setDeleted(false);

            collectMapper.insert(collect);
            return 0;
        } else {
            //已存在取消收藏
            for (Collect collect : collects) {
                collect.setDeleted(true);
                collect.setUpdateTime(new Date());
                collectMapper.updateByPrimaryKeySelective(collect);
            }
            return 1;
        }
    }
}
